# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class FT_07b1:
    
    analysis07b1 = ["催化剂监控:","加热催化剂监控:","蒸发系统监控:","二次空气系统监控:","A/C系统制冷剂监控:","排气传感器加热器监控:","排气传感器加热器监控:","EGR系统和VVT监控:","冷启动辅助系统监控:","增压压力控制系统:","DPF监控:","选择性催化还原系统（SCR）或NOx吸附器:","NMHC氧化催化器监控:","失火监控:","燃油系统监控:","综合成分监控:"]
    analysis07b4_basic = ["自定义消息体长度:", "采集时间:","OBD诊断协议:","MIL状态:","诊断支持状态:","诊断就绪状态:","校准标识符:","标定验证码:","IUPR值:","故障码总数:","故障码列表:"]


    protocolStr = ''
    messageLength = []  # 自定义消息体长度
    collectTime = '' #采集时间
    obdDiagPro = '' #OBD诊断协议
    milStatu = '' #MIL状态
    calid = ''#校准标识符
    cvn = ''#标定验证码
    iupr = ''#iupr
    dm1List = ''#故障码列表
    result = []

    def LogicAisle(self, protocol, header):
        self.result = header
        self.protocolStr = protocol
        self.Init()
        self.result.append(self.analysis07b4_basic[0] + self.messageLength + '\n')    # 自定义消息体长度
        self.result.append(self.analysis07b4_basic[1] + self.collectTime + '\n')   # 采集时间
        self.result.append(self.analysis07b4_basic[2] + self.obdDiagPro + '\n')   # OBD诊断协议
        self.result.append(self.analysis07b4_basic[3] + self.milStatu + '\n')   # MIL状态
        #self.result.append(self.analysis07b4_basic[4] + '\n')   # 诊断支持状态
        diagSupStatu = self.protocolStr[20:24]
        self.result.append(self.analysis07b4_basic[4] + diagSupStatu + '\n')   # 诊断支持状态
        #s = self.hexstrtobin(2,diagSupStatu)
        #s = list(s)
        #print "hexstrtobin.s.type=%s"%type(s)
        #s = s[::-1]
        #flag1 = self.analysis07b1
        #for j in range(len(flag1)):
        #    print s[j]
        #    print type(s[j])
        #    if (s[j] == "1"):
        #        print "1"
        #        self.result.append( flag1[j] + "支持" + '\n')
        #    else:
        #        print "0"
        #        self.result.append( flag1[j] + "不支持" + '\n')
        #self.result.append(self.analysis07b4_basic[5] + '\n')   # 诊断就绪状态
        diagRadStatu = self.protocolStr[24:28]
        self.result.append(self.analysis07b4_basic[5] + diagRadStatu + '\n')   # 诊断就绪状态
        #s = self.hexstrtobin(2,diagRadStatu)
        #s = list(s)
        #s = s[::-1]
        #flag1 = self.analysis07b1
        #for j in range(len(flag1)):
        #    if (s[j] == "1"):
        #        self.result.append( flag1[j] + "支持" + '\n')
        #    else:
        #        self.result.append( flag1[j] + "不支持" + '\n')
        calidStr = self.protocolStr[28:60]
        #self.calid = self.hexgb2312toutf8(calidStr)
        self.calid = calidStr
        self.result.append(self.analysis07b4_basic[6] + self.calid + '\n')   # 校准标识符
        cvnStr = self.protocolStr[60:96]
        #self.cvn = self.hexgb2312toutf8(cvnStr)
        self.cvn = cvnStr
        self.result.append(self.analysis07b4_basic[7] + self.cvn + '\n')   # 标准验证码
        self.iupr = self.protocolStr[96:98]
        self.result.append(self.analysis07b4_basic[8] + self.iupr + '\n')   # iupr
        dm1NumStr = self.protocolStr[98:100]
        self.result.append(self.analysis07b4_basic[9] + str(int(dm1NumStr,16)) + '\n')   # 故障码总数
        if (int(dm1NumStr,16) != 0):
            self.dm1List = self.protocolStr[100:100+int(dm1NumStr,16)*4*2]
            self.result.append(self.analysis07b4_basic[10] + self.dm1List + '\n')   # 故障码列表
        else:
            print "nolist"
            #self.dm1List = self.protocolStr[94:96]
    def Init(self):
        self.setMessageLength()
        self.setCollectTime()
        self.setObdDiagPro()
        self.setMilStatu()

    def setMessageLength(self):
        self.messageLength = self.protocolStr[0:4]
        print"setMessageLength"
    def setCollectTime(self):
        self.collectTime = self.protocolStr[4:16]
        print"setCollectTime"
    def setObdDiagPro(self):
        self.obdDiagPro = self.protocolStr[16:18]
        print"setObdDiagPro"
    def setMilStatu(self):
        self.milStatu = self.protocolStr[18:20]
        print"setMilStatu"
    def hexstrtobin(self,len,str):
    #转换为二进制字符串
        s=bin(int(str,16))
        #位数不足补零
        n=int(s[2:])
        n="%016d"%n
        print "hexstrtobin.n=%s"%n
        print "hexstrtobin.n.type=%s"%type(n)
        #s=str(n)
        #print "hexstrtobin.s.type=%s"%type(s)
        return n
    def hexgb2312toutf8(self,str):
        str = str.decode("hex").decode('gb2312')
        return str
